class_name ReloadComponent_Weapon
extends BaseComponent

#region 属性
@export var is_show_reload_progress_bar:bool
@export var reload_progress_bar_offest:Vector2
@export var reload_progress_bar:WeaponReloadProgressBar
@export var start_reload_additional_effect:Array[AdditionalEffect]
@export var end_reload_additional_effect:Array[AdditionalEffect]
var weapon:Weapon
var weapon_data:EntityData_Weapon
var connect_obj_dict:Dictionary = {}
var enable:bool = false
var is_reloading:bool = false
var reload_progress_bar_original_position:Vector2 = Vector2.ZERO
#endregion
#region 字段
#endregion
#region 信号
signal on_start_reload
signal on_end_reload
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_component(args:Dictionary = {}):
	super(args)
	self.weapon = args.get("Weapon")
	self.weapon_data = self.weapon.get_weapon_data()
	connect_obj_dict["WeaponStateChanged"] = ConnectObj.new(weapon.weapon_state_changed,[on_weapon_state_changed])
	is_reloading = false
	reload_progress_bar.visible = false
	reload_progress_bar_original_position = reload_progress_bar.global_position
	for effect:AdditionalEffect in start_reload_additional_effect:
		effect.init({Target = weapon,Owner = self})
	for effect:AdditionalEffect in end_reload_additional_effect:
		effect.init({Target = weapon,Owner = self})
	inited = true
func obtain(entity:Entity):
	pass
func discard():
	pass
func restore_pickable():
	pass
func equip():
	if reload_progress_bar and is_show_reload_progress_bar:
		if weapon.owner_entity and weapon.owner_entity.get_component(SlotComponent):
			var head:Marker2D = weapon.owner_entity.get_component(SlotComponent).get_slot_manager().get_slot_by_type(GameEnum.SlotType.Head)
			reload_progress_bar.reparent(head)
			reload_progress_bar.position = Vector2.ZERO
		reload_progress_bar.global_position += reload_progress_bar_offest
func unequip():
	end_reload()
	if reload_progress_bar and is_show_reload_progress_bar:
		reload_progress_bar.reparent(self)
		reload_progress_bar.position = reload_progress_bar_original_position
func destroy():
	for connect_obj:ConnectObj in connect_obj_dict.values():
		connect_obj.disconnect_all()
func on_weapon_state_changed(last_weapon_state:GameEnum.WeaponState,cur_weapon_state:GameEnum.WeaponState):
	match cur_weapon_state:
		GameEnum.WeaponState.UnPickable:
			discard()
		GameEnum.WeaponState.Pickable:
			restore_pickable()
		GameEnum.WeaponState.Obtain:
			obtain(weapon.get_weapon_entity())
		GameEnum.WeaponState.UnEquip:
			unequip()
		GameEnum.WeaponState.Equip:
			equip()
		GameEnum.WeaponState.Destroy:
			destroy()
		_:
			pass
func get_connect_obj_dict() -> Dictionary:
	return connect_obj_dict
func set_enable(enable):
	if self.enable != enable:
		self.enable = enable
func start_reload():
	if is_reloading:
		return
	is_reloading = true
	for effect:AdditionalEffect in start_reload_additional_effect:
		effect.excute()
	if not reload_progress_bar:
		_on_reload_start()
		return
	## TODO 这个ReloadTime的获取日后建议改成给start_reload函数传入参数代替
	reload_progress_bar.init_pb(weapon_data.get_attribute_value("ReloadTime",0),true,_on_reload_start,_on_reload_completed)
	reload_progress_bar.start()
func end_reload():
	if not is_reloading:
		return
	for effect:AdditionalEffect in end_reload_additional_effect:
		effect.excute()
	if not reload_progress_bar:
		_on_reload_completed()
		is_reloading = false
		return
	reload_progress_bar.end()
	is_reloading = false
func reload():
	weapon_data = weapon.get_weapon_data()
	var attr:Attribute = weapon_data.find_attribute("Capacity")
	if not attr or attr.get_value() < 0:
		return
	if weapon_data.get_attribute_value("CapacityPerClip") < 0:
		var cap = max(0,weapon_data.get_attribute_value("MaxCapacity"))
		attr.force_set_base_value(-1 if cap < 0 else attr.get_value() + cap)
		weapon_data.find_attribute("MaxCapacity").force_set_base_value(weapon_data.get_attribute_value("MaxCapacity") - cap)
		return
	var cap = min(
		max(0,
			weapon_data.get_attribute_value("CapacityPerClip") - attr.get_value() if weapon_data.get_attribute_value("ReloadCapacity") < 0 else min(weapon_data.get_attribute_value("ReloadCapacity"), weapon_data.get_attribute_value("CapacityPerClip") - attr.get_value())
			),
		max(
			weapon_data.get_attribute_value("CapacityPerClip") if weapon_data.get_attribute_value("ReloadCapacity") < 0 else min(weapon_data.get_attribute_value("ReloadCapacity"), weapon_data.get_attribute_value("CapacityPerClip")),
			weapon_data.get_attribute_value("MaxCapacity")
			)
		)
	weapon_data.set_cost("MaxCapacity",-(cap if cap >= 0 else 0))
	weapon_data.set_cost("Capacity",cap)
#endregion
#region 私有方法
func _on_reload_start():
	on_start_reload.emit()
func _on_reload_completed():
	on_end_reload.emit()
#endregion
#region 生命周期函数
#endregion
